<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Permission;

class PermissionController extends Controller
{
    //权限列表
    public function permissionList(Request $request){
        $where = function ($query) use ($request) {
            //权限名
            if ($request->has('name') and $request->name != '') {
                $search = "%" . $request->name . "%";
                $query->where('name', 'like', $search);
            }

        };
        $allPermission=Permission::where($where)->paginate(10);
        return view('admin.permission.permissionList',compact('allPermission'));
    }

    //新建权限
    public function createPermission(Request $request){
        return view('admin.permission.createPermission');
    }
    //保存权限
    public function savePermission(Request $request){
        return Permission::checkPermission($request);
    }

    //编辑权限
    public function editPermission(Request $request){
        $arr=explode('=',$request->permissionId);
        $permissionId=$arr['1'];
        $editPermission=Permission::where('id',$permissionId)->first();
        return view('admin.permission.editPermission',compact('editPermission'));
    }

    //更新权限
    public function updatePermission(Request $request){
        return Permission::updatePermission($request);
    }

    //删除权限
    public function deletePermission(Request $request){
        $deletePermission=Permission::destroy($request->permissionId);
        if($deletePermission){
            $backMsg=['msg'=>200];
            return json_encode($backMsg);
        }
    }
}
